﻿using Sc.Cache;
using Sc.Domain;
using Sc.Persistence;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Service
{
    public class Sys_ToDoListService
    {
        /// <summary>
        /// 待办事宜数据持久层
        /// </summary>
        private Sys_ToDoListDAO dal = new Sys_ToDoListDAO();
        /// <summary>
        /// 消息推送服务
        /// </summary>
        private readonly CommPushMessage cpm = new CommPushMessage();

        #region  BasicMethod
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Sys_ToDoList model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Sys_ToDoList model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            return dal.Delete(Id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            return dal.DeleteList(Idlist);
        }
        #endregion  BasicMethod

        #region UserMethod
        /// <summary>
        /// 保存多条待办事宜
        /// </summary>
        /// <param name="list">数据</param>
        /// <returns></returns>
        public int SaveToDoList(IList<Sys_ToDoList> list)
        {
            var tmp = this.dal.SaveToDoList(list);
            if (tmp > 0)
            {
                var first = list.FirstOrDefault();
                MessageParam ent = new MessageParam();
                RefDataRecord data = new RefDataRecord(first.Source);
                ent.BusinessId = data.ObjectId;
                ent.Perfix = data.Prefix;
                ent.Title = "派车系统通知";
                ent.Content = first.Name + "流程申请";
                ent.UserIosToken = new List<string>();
                ent.UserAndroidToken = new List<string>();
                foreach (var item in list)
                {
                    var mp = ProjectCache.UserToken.ContainsKey(item.UserId);
                    if (mp)
                    {
                        var mg = ProjectCache.UserToken[item.UserId];
                        if (mg.OsType == 0)
                            ent.UserAndroidToken.Add(mg.Token);
                        else
                            ent.UserIosToken.Add(mg.Token);
                    }
                }
                if (ent.UserAndroidToken.Count > 0)
                {
                    cpm.PushMessageForAndroid(ent);
                }
                if (ent.UserIosToken.Count > 0)
                {
                    cpm.PushMessageForIOS(ent);
                }
            }
            return tmp;
        }
        /// <summary>
        /// 获取用车申请列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="pageIndex">当前第几页</param>
        /// <param name="state">状态</param>
        /// <returns></returns>
        public CommModel<Sys_ToDoList> GetList(int userId, int pageSize, int pageIndex, int state)
        {
            Hashtable param = new Hashtable();
            param.Add("A1", pageIndex);
            param.Add("A2", pageSize);
            param.Add("A3", "AND UserId=" + userId + " AND State=" + state);
            param.Add("A4", "Sys_ToDoList");
            param.Add("A5", "CreateTime DESC");
            var ht = CommDAO.CommPagination(param);
            DataTable dt = ht["data"] as DataTable;
            List<Sys_ToDoList> list = new List<Sys_ToDoList>();
            foreach (DataRow item in dt.Rows)
            {
                list.Add(DataRowToModel(item));
            }
            CommModel<Sys_ToDoList> ent = new CommModel<Sys_ToDoList>();
            ent.Data = list;
            ent.PageCount = Convert.ToInt32(ht["PageCount"]);
            ent.RecordCount = Convert.ToInt32(ht["record"]);
            return ent;
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Sys_ToDoList DataRowToModel(DataRow row)
        {
            Sys_ToDoList model = new Sys_ToDoList();
            if (row != null)
            {
                if (row["Id"] != null && row["Id"].ToString() != "")
                {
                    model.Id = int.Parse(row["Id"].ToString());
                }
                if (row["UserId"] != null && row["UserId"].ToString() != "")
                {
                    model.UserId = int.Parse(row["UserId"].ToString());
                }
                if (row["Name"] != null)
                {
                    model.Name = row["Name"].ToString();
                }
                if (row["ActionURL"] != null)
                {
                    model.ActionURL = row["ActionURL"].ToString();
                }
                if (row["UrgencyDegree"] != null && row["UrgencyDegree"].ToString() != "")
                {
                    model.UrgencyDegree = int.Parse(row["UrgencyDegree"].ToString());
                }
                if (row["State"] != null && row["State"].ToString() != "")
                {
                    model.State = int.Parse(row["State"].ToString());
                }
                if (row["CreateTime"] != null && row["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(row["CreateTime"].ToString());
                }
                if (row["UpdateTime"] != null && row["UpdateTime"].ToString() != "")
                {
                    model.UpdateTime = DateTime.Parse(row["UpdateTime"].ToString());
                }
                if (row["CreaterName"] != null)
                {
                    model.CreaterName = row["CreaterName"].ToString();
                }
                if (row["Source"] != null)
                {
                    model.Source = row["Source"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获取当前用户未读消息
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns></returns>
        public int GetNoReadMsgCount(int userId)
        {
            return dal.GetNoReadMsgCountByUserId(userId);
        }
        /// <summary>
        /// 获取详情（单个实体）
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        public Sys_ToDoList GetDetail(int id)
        {
            return this.dal.GetDetail(id);
        }
        /// <summary>
        /// 根据业务ID更新待办事宜
        /// </summary>
        /// <param name="soruce">业务前缀+Id</param>
        /// <param name="state">状态</param>
        /// <returns></returns>
        public bool UpdateAll(string soruce, int state)
        {
            return this.dal.UpdateAll(soruce, state);
        }
        #endregion
    }
}
